// pages/index/business/index.js
import { policyList, chuangyeList, policyImage, policyTitle, publicList, ideaBank, hotyqList, rmhdList, titleStyle, basicservice, basicsCurrent } from "../../../../utils/data/tabbar";
import { yuanquPageList, getAllCoordinates } from  "../../../../utils/api/module/yuanqu";
import { getServiceType, serviceList } from  "../../../../utils/api/module/cyfw";
import { getintro, expertList, getUMlist } from  "../../../../utils/api/module/cylm";

import { articleList, articlePageList} from "../../../../utils/api/module/wenzhang";
import {allColumn} from "../../../../utils/api/module/lanmu"
Page({
    data: {
        gbConfig: getApp().globalData.config,
        statusBarHeight: 0,
        titleBarHeight: 0,
        title: '创业园区',
        subhead: '为您提供优质办公环境 为梦想助力',
        currentData: '',
        ceilingFlag: false,
        titleBar: chuangyeList,
        titleBarIndex: 3,
        // 政策汇
        policyImage,
        policyTitle,
        policyTitleIndex: 0,
        policyList,
        publicList,
        ideaBank: [], // 智库
        hotyqList: [], // 联盟成员
        rmhdList, // 联盟活动
        titleStyle, // title
        page: 1,
        image: getApp().defaultImg.yqImg,   //园区默认图片
        pn: 1,
        isLineData: 0,
        // basicservice, // 服务选择
        serviceData: [], // 热门服务
        // basicsCurrent, // 基础服务
        serviceTypeData: [], // 基础 || 产业 服务内容
        gbConfig: getApp().globalData.config, // 图片前缀域名
        currentDataDetail: '', // 联盟简介
        keywords: ''
    },
    onLoad: function (options) {
        console.log(options)
        this.setData({
            titleBarIndex: Number(options.index),
            title: this.data.titleBar[Number(options.indexs)].name,
            subhead: this.data.titleBar[Number(options.indexs)].subhead
        });
        let data = getApp().statusBarHeight;
        this.setData({
            statusBarHeight: Number(data.statusBarHeight),
            titleBarHeight: data.system.indexOf('IOS') > -1 ? 48 : 44
        })
        this.getPolicyImage();
        this.getLanmuList();
        this.getYuanquPageList();
        this.getServiceType(); // 服务选择
        this.serviceList(); // 热门服务
        this.getintro(); // 联盟简介
        this.expertList(); // 联盟智库接口
        this.getUMlist();
        this.getAllCoordinates(); // 获取园区坐标
    },
    // 获取园区坐标
    getAllCoordinates() {
        getAllCoordinates().then(res => {
            let data = res.data.data,
                array = [];
            data.map((item, index) => {
                item.coordinates = item.coordinates.split(",")
                array.push({
                    iconPath: "/assets/img/local09.png",
                    id: index,
                    ids: item.id,
                    latitude: item.coordinates[0],
                    longitude: item.coordinates[1],
                    name: item.title,
                    width: 30,
                    height: 30,
                    // callout: 
                    label: {
                        anchorX: 0,
                        anchorY: -62,
                        textAlign:'center',
                        bgColor: "#ffffff",
                        borderColor:"#ffffff",
                        content: item.title,
                        color:"#000000",
                        color: '#333333',
                        fontSize: 14,
                        borderRadius: 6,
                        borderWidth: 6
                    }
                })
              })
              console.log(array, 'array')
            this.setData({ mapData: array });
        })
    },
    // 地图跳转
    bindmarkertap(e) {
        let index = e.detail.markerId;
        wx.navigateTo({
          url: '/pages/index/business/detail/index?id=' + this.data.mapData[index].ids,
        });
    },
    input: function(e) {
        this.setData({
            keywords: e.detail.value
        });
    },
    getInputVal() {
        this.data.page = 1;
        this.getYuanquPageList();
    },
    // 页面滚动
    onPageScroll: function(e) {
        let scrollTop = e.scrollTop;
        if(scrollTop > 166) this.setData({ ceilingFlag: true });
        if(scrollTop < 166) this.setData({ ceilingFlag: false })
    },
    // 页面跳转
    go: function(e) {
        let url = e.currentTarget.dataset.url;
        wx.navigateTo({ url: url });
    },
    // 政策汇 scroll-vive
    bindOption: function(e) {
        let index = e.currentTarget.dataset.index;
        this.setData({ policyTitleIndex: index });
        if(this.data.policyTitle[index].currentData.length == 0) this.getYqList();
    },
    // 主导航栏
    bindDirect: function(e) {
        let data = e.currentTarget.dataset;
        this.setData({ titleBarIndex: data.index, title: this.data.titleBar[Number(data.indexs)].name});
        if(this.data.ceilingFlag) {
            wx.pageScrollTo({ scrollTop: 166 });
        };
        console.log(this.data.titleBar[Number(data.indexs)])
    },
    // 返回
    back: function() {
        wx.navigateBack({ delta: 1 });
    },
    /**
     * 获取园区列表数据
     */
    getYqList(){
        articlePageList({
            columnid: this.data.policyTitle[this.data.policyTitleIndex].id,
            limit: 5,
            page: this.data.page
        }).then(res => {
            this.setData({
                ["policyTitle[" + this.data.policyTitleIndex + "].currentData"]: res.data.data.rows,
                policyTitle: this.data.policyTitle
            });
        })
    },
    // 栏目
    getLanmuList(){
        allColumn({
            "parentid": 1
        }).then(res => {
            let data = res.data.data;
            data.map(item => {
                item.currentData = [];
            });
            this.data.policyTitle = data;
            this.getYqList();
        })
    },
    // 最新政策
    getPolicyImage: function() {
        articleList({
            columnid: 1,
            limit: 5,
            isrec: 0
        }).then(res => {
            this.setData({
                policyImage: res.data.data
            })
        })
    },
    // 园区
    getYuanquPageList: function() {
        yuanquPageList({
            page: this.data.pn,
            limit: 10,
            keywords: this.data.keywords
        }).then( res => {
            let data = res.data.data.rows
            this.setData({
                ['currentData[' + (this.data.pn - 1) + ']'] : data,
                isLineData: this.data.pn !== 1 && data.length < 10 ? 2 : 0
            });
        });
    },
    // 获取联盟成员
    getUMlist: function() {
        getUMlist({
            limit: 4
        }).then(res => {
            this.setData({ hotyqList: res.data.data })
        });
    },
    onReachBottom: function() {
        if(this.data.titleBarIndex === 0 && this.data.currentData[this.data.pn - 1].length == 10) {
            this.data.pn += 1;
            this.setData({ isLineData: 1 });
            this.getYuanquPageList();
        }else{
            this.setData({ isLineData: 2 });
        };
    },
    // 创业联盟
    // 联盟简介
    getintro: function() {
        getintro().then(res => {
            this.setData({ currentDataDetail: res.data.data })
        })
    },
    // 联盟智库接口
    expertList: function() {
        expertList().then(res => {
            this.setData({ ideaBank: res.data.data })
        });
    },
    // 创业服务
    // 服务选择
    getServiceType: function(id, index) {
        if(!id) id = 0;
        getServiceType({
            parent: id
        }).then(res => {
            if(!index && !id) {
                this.data.serviceTypeData = res.data.data;
                this.data.serviceTypeData.map((item, index) => {
                    item.children = [];
                    this.getServiceType(item.id, index);
                })
                this.setData({ serviceTypeData: this.data.serviceTypeData })
            }else{
                this.setData({
                    ['serviceTypeData[' + index + '].children']: res.data.data
                });
            }
        })
    },
    // 热门服务
    serviceList: function() {
        serviceList({
            type: 1,
            limit: 5
        }).then( res => {
            this.setData({
                serviceData: res.data.data
            })
        })
    },
    bindGo: function(e) {
        let data = e.currentTarget.dataset.id;
        wx.navigateTo({
          url: '/pages/index/business/service/index?id=' + data,
        })
    },
    // 点击搜索框
    bindShare: function() {
        if( this.data.titleBarIndex == 2) {
            let data = this.data.policyTitle[this.data.policyTitleIndex];
            console.log(data, 1111)
            wx.navigateTo({
              url: '/pages/index/business/policy/index?id=' + data.id + '&title=' + data.name + "&type=2",
            })
            return false;
        };
        // if( this.data.titleBarIndex == 2) {
        //     wx.navigateTo({
        //       url: '/pages/index/business/service/index',
        //     })
        //     return false;
        // };
        if( this.data.titleBarIndex == 0) {
            this.setData({
                focus: true
            })
            return false;
        };
    }
})